'use strict';

const Controller = require('egg').Controller;
class UserController extends Controller {
    //渲染登录页面
    async login() {
        await this.ctx.render('login')
    }
    //判断登录
    async dologin() {
        const { ctx, app } = this;
        const { name } = ctx.request.body;
        const post = await app.knex.first("*").where({ name: name }).from('user')
        if (post) {
            this.ctx.session.username=name;
            await ctx.render('index', {
                username: name
            })
        } else {
            const result = await app.knex.insert({ name: name }).into('user')
            this.ctx.session.username=name;
            await ctx.render('index', {
                username: name
            })
        }

    }
    //
    async register() {
        const { username } = this.ctx.query;
        if (username === '') {
            this.ctx.body = { result: 1, msg: '用户名不能为空!' }
            return;
        }
        this.ctx.session.username = username;
        return this.ctx.body = { result: 0 };
    }
}
module.exports = UserController